var express = require('express');
var router = express.Router();
var db = require('../config/db.config');
var querystring = require('querystring')
router.get('/', (req, res) => {
  db.query('SELECT * from users', (err, data) => {
    if (err) {
      res.send({ code: 0, msg: "error", data: [] })
    } else {
      res.send({ code: 200, msg: "success", data })
    }
  })
})
router.post('/post', (req, res) => {
  // 获取请求数据
  const username = req.body.username
  const password = req.body.password
  console.log(username)
  db.query(`select * from users where username = '${username}' and password = '${password}' `, (err, data) => {
    if (err) {
      res.send({ code: 0, msg: "error", data: [] })
    } else {
      res.send({ code: 200, msg: "success", data })
    }
  })
})
router.post('/get', (req, res) => {
  var post = ''
  req.on('data', function (chunk) {
    post += chunk
    console.log(post)
  })
  req.on('end', function () {
    post = querystring.parse(post)
    db.query(`select * from users where username = '${post.username}' and password = '${post.password}' `, (err, data) => {
      if (err) {
        res.send({ code: 0, msg: "error", data: [] })
      } else {
        res.send({ code: 200, msg: "success", data })
      }
    })
  })
})
module.exports = router